//201902104003邵潇漩
package bysj.service;

import bysj.dao.ProfTitleDao;
import bysj.domain.ProfTitle;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
/**
 * @author 邵潇漩
 * @version 1.0  11-13-2020
 */

public final class ProfTitleService {
	private static ProfTitleDao profTitleDao= ProfTitleDao.getInstance();
	private static ProfTitleService profTitleService=new ProfTitleService();
	private ProfTitleService(){}

	public static ProfTitleService getInstance(){
		return profTitleService;
	}

	public Collection<ProfTitle> findAll(Pagination pagination,List<Condition> conditionList) throws SQLException {
		//获得连接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		Collection<ProfTitle> profTitles=null;
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		try {
			profTitles = profTitleDao.findAll(pagination,conditionList,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(connection);
		}
		return profTitles;
	}

	public ProfTitle find(Integer id) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		ProfTitle profTitle=null;
		try {
			profTitle = profTitleDao.find(id,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(connection);
		}
		return profTitle;
	}

	public boolean update(ProfTitle profTitle) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		boolean title_update=false;
		try {
			title_update = profTitleDao.update(profTitle,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(connection);
		}
		return title_update;
	}

	public boolean add(ProfTitle profTitle) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		boolean title_add=false;
		try {
			title_add = profTitleDao.add(profTitle,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(connection);
		}
		return title_add;
	}

	public boolean delete(Integer id) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		boolean title_del=false;
		try {
			title_del = profTitleDao.delete(id,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(connection);
		}
		return title_del;
	}
}

